package com.dhcc.sds.busi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.cdc.busi.service.ICdcHospitalService;
import com.dhcc.cdc.busi.service.ICdcLocationService;
import com.dhcc.cdc.busi.service.ICdcUserService;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsQcentityService;
import com.dhcc.sds.busi.service.IAnalysisSdsPortalService;
import com.dhcc.sds.busi.service.ISdsQcincaseOperlogService;
import com.dhcc.sds.busi.service.ISdsQcincaseService;
import com.dhcc.sds.busi.vo.AnalysisSdsPortalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 单病种门户
 *
 * @ClassName: AnalysisSdsPortalController
 * @Description: TODO
 * @author zhangdc
 * @date 2022-03-22 10:09:15
 */
@Controller
@RequestMapping("${sys.backendPath}/analysis/portal")
public class AnalysisSdsPortalController {

    private String PREFIX = "/backend/sds/portal/";

    @Autowired
    private IAnalysisSdsPortalService analysisSdsPortalService;
    @Autowired
    private ICdcHospitalService cdcHospitalService;
    @Autowired
    private ICdcLocationService cdcLocationService;
    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    private ICdcUserService cdcUserService;
    @Autowired
    private ISdsQcentityService sdsQcentityService;
    @Autowired
    private ISdsQcincaseService sdsQcincaseService;
    @Autowired
    private ISdsQcincaseOperlogService sdsQcincaseOperlogService;

    /**
     * @Description: 单病种门户
     * @Title:
     * @author: zhangdc
     * @date:2022-03-22 10:09:15
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "portal.html";
    }

    /**
     * @Description: 查询近一年单病种上报情况
     * @Title: selectMainChart
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectMainChart")
    @ResponseBody
    public Object selectMainChart(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        return analysisSdsPortalService.selectMainChart(params);
    }

    /**
     * @Description: 查询单病种上报总体情况
     * @Title: selectMrlistCnt
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectMrlistCnt")
    @ResponseBody
    public Object selectMrlistCnt(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        return analysisSdsPortalService.selectMrlistCnt(params);
    }

    /**
     * @Description: 查询单病种费用/住院日
     * @Title: selectSysSumChart
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectSysSumChart")
    @ResponseBody
    public Object selectSysSumChart(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        return analysisSdsPortalService.selectSysSumChart(params);
    }

    /**
     * @Description: 查询病种上报情况
     * @Title: selectQCSumChart
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectQCSumChart")
    @ResponseBody
    public Object selectQCSumChart(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        return analysisSdsPortalService.selectQCSumChart(params);
    }


    /**
     * @Description: 查询科室上报情况
     * @Title: selectLocSumChart
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectLocSumChart")
    @ResponseBody
    public Object selectLocSumChart(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        return analysisSdsPortalService.selectLocSumChart(params);
    }


    /**
     * @Description: 查询医生上报情况
     * @Title: selectDocSumChart
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectDocSumChart")
    @ResponseBody
    public Object selectDocSumChart(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        return analysisSdsPortalService.selectDocSumChart(params);
    }


    /**
     * @Description: 查询病种自动填报率分析
     * @Title: selectARatioChart
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectARatioChart")
    @ResponseBody
    public Object selectARatioChart(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        return analysisSdsPortalService.selectARatioChart(params);
    }


    /**
     * @Description: 查询病种填报用时分析
     * @Title: selectUseTimeChart
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectUseTimeChart")
    @ResponseBody
    public Object selectUseTimeChart(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        return analysisSdsPortalService.selectUseTimeChart(params);
    }


    /**
     * 跳转到关注病例列表
     */
    @RequestMapping("list")
    public String list(@RequestParam Map<String, Object> params, Model model) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        //获取部门信息
        JSONArray officMap = cdcHospitalService.getOfficeMap();
        //默认加载所有科室
        //JSONArray departMap = cdcLocationService.getDepartMap();
        //加载用户关联科室
        JSONArray departMap = sdsCommonService.getDepartMap(new HashMap<>());
        //状态
        JSONArray statusMap = sdsCommonService.getDicCodeArray("SDQCMrStatus");
        //主任医师
        JSONArray docMap = cdcUserService.getUserMap("DOCTOR5");
        //获取病种
        JSONArray entityMap = sdsQcentityService.getUpEntityMap();
        String idName = params.get("idName").toString();
        List<AnalysisSdsPortalVo> sdsQcentityEntities;
        if (Objects.equals("LocSumMore", idName) || Objects.equals("DocSumMore", idName) || Objects.equals("QCSumMore", idName)){
            //sdsQcentityEntities = sdsQcincaseOperlogService.selectLocSumChartData(params);
            sdsQcentityEntities = analysisSdsPortalService.selectSumChartData(idName, params);
        }else {
            sdsQcentityEntities = analysisSdsPortalService.selectMrlistCntDetails(idName, deptIds);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", sdsQcentityEntities);
        model.addAttribute("data", jsonObject);
        model.addAttribute("officeMap",officMap);
        model.addAttribute("departMap",departMap);
        model.addAttribute("statusMap",statusMap);
        model.addAttribute("docMap",docMap);
        model.addAttribute("entityMap",entityMap);
        model.addAttribute("idName",idName);
        model.addAttribute("hospitalId",sdsCommonService.getHospIdBySysUser());
        return PREFIX +  "portalMrlist.html";
    }


    /**
     * @Description: 查询三级页面数据
     * @Title: selectMrInfoByIDs
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectMrInfoByIDs")
    @ResponseBody
    public Object selectMrInfoByIDs(@RequestParam Map<String, Object> params) {
        return analysisSdsPortalService.selectMrInfoByIDs(params);
    }

    /**
     * 跳转到病种填报用时分析列表
     */
    @RequestMapping("useTime")
    public String useTime(@RequestParam Map<String, Object> params, Model model) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        JSONObject jsonObject = analysisSdsPortalService.selectUseTimeChart(params);
        model.addAttribute("data", jsonObject);
        return PREFIX +  "portalUseTime.html";
    }

    /**
     * 跳转到病种自动填报率分析列表
     */
    @RequestMapping("autoRatio")
    public String autoRatio(@RequestParam Map<String, Object> params, Model model) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        JSONObject jsonObject = analysisSdsPortalService.selectARatioChart(params);
        model.addAttribute("data", jsonObject);
        return PREFIX +  "portalAutoRatio.html";
    }


    /**
     * @Description: 查询自动填报率数据
     * @Title: getAutoAlias
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("getAutoAlias")
    @ResponseBody
    public Object getAutoAlias(@RequestParam Map<String, Object> params) {
        String id = (String) params.get("mrid");
        return sdsQcincaseService.selectById(Long.parseLong(id));
    }


    /**
     * @Description: 根据条件查询关注病例列表数据
     * @Title: selectByCondition
     * @return: Object
     * @author: zhangdc
     * @date:2022-03-23 10:09:15
     */
    @RequestMapping("selectByCondition")
    @ResponseBody
    public Object selectByCondition(@RequestParam Map<String, Object> params) {
        JSONObject jsonObject = new JSONObject();
        List<AnalysisSdsPortalVo> sdsQcentityEntities = analysisSdsPortalService.selectByCondition(params);
        jsonObject.put("data", sdsQcentityEntities);
        return jsonObject;
    }
}
